﻿using SharpDX;
using System;
using System.Linq;

namespace MikadoShooter
{
    public class Vector4Bg : Vector4Base
    {
        private readonly eWall[] wallOrder;

        public Vector4Bg(int verticals) : base(verticals)
        {
            wallOrder = new eWall[Enum.GetNames(typeof(eWall)).Length];

            int index = 0;
            AddLine(ref index, Background.GetLine(eWall.Left), eWall.Left);
            AddLine(ref index, Background.GetLine(eWall.DownLeft), eWall.DownLeft);
            AddLine(ref index, Background.GetLine(eWall.Right), eWall.Right);
            AddLine(ref index, Background.GetLine(eWall.DownRight), eWall.DownRight);
            AddLine(ref index, Background.GetLine(eWall.Down), eWall.Down);
            AddLine(ref index, Background.GetLine(eWall.Up), eWall.Up);
            AddLine(ref index, Background.GetLine(eWall.EndLine), eWall.EndLine);

            if (index != verticals * 2)
                throw new ApplicationException("Error CalcVerticals in drawbg..");
        }

        public int GetIndex(eWall lineName)
        {
            var data = wallOrder.Select((item, pos) => new {Item = item, Position = pos}).SingleOrDefault(x => x.Item == lineName);
            return data == null ? 0 : data.Position;
        }

        private void AddLine(ref int index, Background.BackgroundLines line, eWall lineName)
        {
            //Screenborder fix
            Vector4 borderFix = Vector4.Zero;
            switch (lineName)
            {
                case eWall.Left:
                    borderFix = new Vector4(-0.001f, -0.001f, 0.0f, 0.0f);
                    break;
                case eWall.Up:
                    borderFix = new Vector4(0.001f, 0.001f, 0.0f, 0.0f);
                    break;
            }

            Data[index++] = line.Start - borderFix;
            Data[index++] = StdColors.WhiteColor;
            Data[index++] = line.End - borderFix;
            Data[index++] = StdColors.WhiteColor;

            wallOrder[(index / 4) -1] = lineName;
        }
    }
}